package com.jxt.myjxtmanager.db;

import com.jxt.myjxtmanager.db.dao.SubjectDao;

import java.util.Collection;
import java.util.List;

import de.greenrobot.dao.query.LazyList;

/**
 * Created by qm on 2017/3/7 0007.
 */
public class SubjectModel {
    private static SubjectModel instance;
    private SubjectDao subjectDao;

    public synchronized static SubjectModel getInstance() {
        if (instance == null) {
            instance = new SubjectModel();
        }
        return instance;
    }

    private SubjectModel() {
        subjectDao = DBHelper.getInstance().getDaoSession()
                .getSubjectDao();
    }

    public boolean insertOrReplace(Subject message) {
        subjectDao.insertOrReplace(message); // 30ms
        return true;
    }

    public boolean update(Subject message) {
        subjectDao.update(message); // 30ms
        return true;
    }

    public void insertOrReplaceAll(Collection<Subject> message) {
        subjectDao.insertOrReplaceInTx(message);
    }

    public LazyList<Subject> lazyLoadmessage() {
        return subjectDao.queryBuilder().listLazy();
    }

    public Subject getMessageByName(String subName) {
        return subjectDao.queryBuilder()
                .where(SubjectDao.Properties.SubName.eq(subName)).limit(1)
                .unique();
    }

    public void deleteAll() {
        subjectDao.deleteAll();
    }

    public void deleteSubject(Subject empty) {
        subjectDao.delete(empty);
    }

    public List<Subject> getMessageID(String Id) {
        return subjectDao.queryBuilder().where(SubjectDao.Properties.Id.eq(Id)).list();
    }

}
